var Calendar={
	url : "call/route_Calendar_Ajax.php",
	popBox:new PopWindow(),
	init:{
			timeElement:null,
			handles:[],
			odate : ""
		},
	data:{},
	datePrice:{},
	dateStorage:{},
	strpad:function(str){
		str=String(str);
		if(str.length==1){
			return "0"+str;
		}else{
			return str;
		}
	},
	outputHtml:function(json,yearmonth,fromDate){
		var d={};
		for(r in json){
			d[r.date]=r.price;
		}		
		var nowtime=new Date();
			if(fromDate){
				fromDate=fromDate.split("-");
				fromDate=fromDate.join("/");
				if((new Date(fromDate)).getTime()>nowtime)
				 nowtime=(new Date(fromDate));
			}
			nowtime.setHours(0,0,0,0);
			nowtime=nowtime.getTime();//凌晨时间
			
		var begintime=yearmonth+"-01";
			begintime=begintime.split("-");
			
			begintime=begintime.join("/");
			
		var beginDate=new Date(begintime);
		var year=beginDate.getFullYear();
		var month=beginDate.getMonth()+1;
		var lastmonth=(month-1==0)?12:month-1;
		var nextmonth=(month+1==13)?1:month+1;
		var days=(new Date(beginDate.getFullYear(),beginDate.getMonth()+1,0)).getDate();
		//console.log(beginDate.getDate())
		//console.log(new Date(2014,10,0).getDate())
		var monthdays=days;
		var emptydays=beginDate.getDay();
		var endtime=yearmonth+"-"+days;
			endtime=endtime.split("-");
			endtime=endtime.join("/");
		var endDate=new Date(endtime);
		
		days+=beginDate.getDay()+(7-endDate.getDay());
		//console.log(days);
		beginDate.setTime(beginDate.getTime()-(24*3600000*beginDate.getDay()));
		var lastmonth_none=(json.about.mintime*1000-beginDate.getTime()<0)?"":"lastmonth_none";
		var nextmonth_none=(json.about.maxtime*1000-endDate.getTime()>0)?"":"nextmonth_none";
		var html='<div class="calendar" id="calendarContent"><div class="monthbox">'+
					'<div class="title"><span class="lastmonth '+lastmonth_none+'"><a title="'+lastmonth+'月">&nbsp;</a></span><span class="nextmonth '+nextmonth_none+'"><a title="'+nextmonth+'月">&nbsp;</a></span><span class="year">'+year+'年'+month+'月</span><span class="close"></span></div>'+
						'<table>'+
							'<tr>'+
								'<th class="weeken">日</th>'+
								'<th>一</th>'+
								'<th>二</th>'+
								'<th>三</th>'+
								'<th>四</th>'+
								'<th>五</th>'+
								'<th class="weeken">六</th>'+
							'</tr>';
		for(var i=0,j=0;i<days-1;i++){
			if(i%7==0){
				html+='<tr>';
			}
			//console.log(beginDate.getDate())
			var date=beginDate.getFullYear()+"-"+this.strpad((beginDate.getMonth()+1))+"-"+this.strpad(beginDate.getDate());
			var price="";
			var priceT="";
			var valid="";
			var validT="";
			var remain;
			if(json.pricelists[j]&&json.pricelists[j].date==date){
				this.datePrice[date]=json.pricelists[j].price;
				this.dateStorage[date]=json.pricelists[j].storage;
				price=json.pricelists[j].price;
				priceT=json.pricelists[j].price?"¥"+json.pricelists[j].price:"";
				priceT="¥"+json.pricelists[j].price;
				valid="valid";
				j++;
			}
			if(i<emptydays||i>=monthdays+emptydays){
				html+='<td><div class="detail"></div></td>';
			}else if(beginDate.getTime()<nowtime){
				html+='<td><div data-price="'+price+'" date="'+date+'" class="detail"><span>'+beginDate.getDate()+'</span><div class="price"></div></div></td>';
			}else{
				html+='<td><div data-price="'+price+'" date="'+date+'" class="detail '+valid+'"><span>'+beginDate.getDate()+'</span><div class="price">'+priceT+validT+'</div></div></td>';
			}
			if(i%7==6){
				html+='</tr>';
			}
			beginDate.setTime(beginDate.getTime()+24*3600000);
		}
		html+="</table></div>";
		return html;
	},
	show:function(yearmonth,pid,fromDate,aid){
		var html;
		if(false){//this.data[yearmonth]
			html=this.outputHtml(this.data[yearmonth],yearmonth,fromDate);
		}else{
			$.ajax({
				url:Calendar.url,
				async:false,
				type:"GET",
				dataType:"json",
				data:{"orderMonth":yearmonth,"pid":pid, "aid":aid},
				success:function(json){
					html=Calendar.outputHtml(json,yearmonth,fromDate);
					Calendar.data[yearmonth]=json;
				}
			})
		}
		Calendar.popBox.open(html);
		$("#calendarContent div.valid").click(function(){//选择日期
			Calendar.init.timeElement.value=$(this).attr("date");
            //触发change事件_added by cgp BEGIN
            $(Calendar.init.timeElement).trigger('change');
            //触发change事件_added by cgp END
			Calendar.popBox.close();
			for(var i=0;i<Calendar.init.handles.length;i++){
				Calendar.init.handles[i](this);
			}	
		})
		$("#calendarContent span.close").click(function(){
			Calendar.popBox.close();
		})
		var time=yearmonth+"-01";
			time=time.split("-");
			time=time.join("/");
			time=new Date(time);
		var lasttime=new Date(time.getFullYear(),time.getMonth()-1,1);
		var nexttime=new Date(time.getFullYear(),time.getMonth()+1,1);
		var lastmonth=lasttime.getFullYear()+"-"+this.strpad(lasttime.getMonth()+1);
		var nextmonth=nexttime.getFullYear()+"-"+this.strpad(nexttime.getMonth()+1);
		$("#calendarContent .lastmonth").click(function(){
			Calendar.show(lastmonth,pid,fromDate,aid);
		})
		$("#calendarContent .nextmonth").click(function(){
			Calendar.show(nextmonth,pid,fromDate,aid);
		})
	},
	nextday : function(){
		var date = new Date();
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		var a = new Date(y,m,0);
		var maxDay = a.getDate();
		var nextday = d+1;
		if(nextday>maxDay){
			nextday = "01";
			m++;
			if(m>12){
				m = "01";
				y++;
			}
		}
		return y+"-"+m+"-"+nextday;
	},
	gettoday : function(){
		var date = new Date();
		var y = date.getFullYear();
		var m = date.getMonth()+1;
		var d = date.getDate();
		return y+"-"+m+"-"+d;
	} 
}; 